package com.wq.jdk8;

import java.util.*;

/**
 * @Author: wangqiang20995
 * @Date:2019/2/15
 * @Description:
 * @Resource:
 */
public class Compare {

    private class Student{

        public Student() {
        }

        public Student(String className, String name, Integer score) {

            this.className = className;
            this.name = name;
            this.score = score;
        }

        private String className;

        private String name;

        private Integer score;

        public String getClassName() {
            return className;
        }

        @Override
        public String toString() {
            return "Student{" +
                    "className='" + className + '\'' +
                    ", name='" + name + '\'' +
                    ", score=" + score +
                    '}';
        }

        public void setClassName(String className) {
            this.className = className;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public Integer getScore() {
            return score;
        }

        public void setScore(Integer score) {
            this.score = score;
        }
    }

    final String classOne = "一班";
    final String classTwo = "二班";
    final String classThree = "三班";
    final String classFour = "四班";
    final String classFive = "五班";

    List<Student> students = new ArrayList<>();

    {
        students.add(new Student(classFive, "老九门", 89));
        students.add(new Student(classOne, "李一", 43));
        students.add(new Student(classTwo, "刘子", 97));
        students.add(new Student(classOne, "张三", 91));
        students.add(new Student(classTwo, "郭子", 59));
        students.add(new Student(classThree, "陈六", 66));
        students.add(new Student(classOne, "王二", 75));
        students.add(new Student(classTwo, "潘子", 88));
        students.add(new Student(classThree, "唐七", 77));
        students.add(new Student(classFive, "狗十三", 89));
        students.add(new Student(classThree, "周八", 89));
    }

    public List<Student> getStudents() {
        return students;
    }

    public static void main(String args[]){

        System.out.println("测试根据某个字段排序。。。。。。。。。。。。。");
        Comparator<Student> comparator = (o1,o2) -> o2.getScore().compareTo(o1.getScore());
        Compare compare = new Compare();
        Collections.sort(compare.getStudents(),comparator);
        System.out.println(compare.getStudents());

        System.out.println("测试对所有数据分组排序。。。。。。。。。。。。。");
        Comparator<Student> groupComparator = (o1,o2) -> {
            int diff = o1.getClassName().compareTo(o2.getClassName());
            return diff == 0 ? o2.getScore().compareTo(o1.getScore()) : diff;
        };

        Compare groupCompare = new Compare();
        Collections.sort(groupCompare.getStudents(),groupComparator);
        System.out.println(groupCompare.getStudents());

        System.out.println("测试对所有数据分组排序，并且各组之间按照制定的顺序排列。。。。。。。。。。。。。。");
        Compare orderCompare = new Compare();
        Map<String,Integer> map = new HashMap<>(4);
        map.put(orderCompare.classOne,1);
        map.put(orderCompare.classTwo,2);
        map.put(orderCompare.classThree,3);
        map.put(orderCompare.classFour,4);
        map.put(orderCompare.classFive,5);
        Comparator<Student> orderComparator = (o1,o2) -> {
            int diff = map.get(o1.getClassName()).compareTo(map.get(o2.getClassName()));
            return diff == 0 ? o2.getScore().compareTo(o1.getScore()) : diff;
        };
        Collections.sort(orderCompare.getStudents(),orderComparator);
        System.out.println(orderCompare.getStudents());


    }

}
